package com.lancoo.znbkxx.teacher.aibkGenerateTestPaper.aibkGeneratePaper

import android.content.Context
import android.content.Intent
import android.os.Handler
import android.os.IBinder
import android.util.Log
import android.view.MotionEvent
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.aibkGenerateTestPaper.aibkCustomTemplet.AibkCustomTempletActivity
import com.lancoo.znbkxx.teacher.aibkGenerateTestPaper.aibkGenerateDialog.DialogGenerateTestpaperProgress
import com.lancoo.znbkxx.teacher.aibkGenerateTestPaper.aibkGenerateDialog.DialogSpecificGrammarAnalysis
import com.lancoo.znbkxx.teacher.aibkGenerateTestPaper.aibkGenerateDialog.DialogSpecificLanguageAnalysis
import com.lancoo.znbkxx.teacher.aibkGenerateTestPaper.aibkGeneratePaper.itemtouchhelper.*
import com.lancoo.znbkxx.teacher.aibkGenerateTestPaper.aibkPreviewTemplet.AibkTempletPreviewActivity
import com.lancoo.znbkxx.teacher.aibkGenerateTestPaper.aibkPreviewTemplet.AibkTempletPreviewLookActivity
import com.lancoo.znbkxx.teacher.aibkGenerateTestPaper.aibkReviewTestPaper.AibkTestPaperReviewActvity
import com.lancoo.znbkxx.teacher.aibkTestCoach.aibkTools.MyToast
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseActivity
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.bindLifeCycle
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.entity.*
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.viewModel.MarkViewModel
import com.luck.picture.lib.decoration.GridSpacingItemDecoration
import com.luck.picture.lib.tools.ScreenUtils
import kotlinx.android.synthetic.main.aibk_activity_generate_paper.*
import kotlinx.android.synthetic.main.aibk_include_layout_test_difficulty_set.*
import kotlinx.android.synthetic.main.aibk_include_layout_test_formplate_choose.*
import kotlinx.android.synthetic.main.aibk_include_layout_test_name.*
import kotlinx.android.synthetic.main.aibk_include_layout_test_specific_choose.*
import kotlinx.android.synthetic.main.aibk_include_loading_refresh.*
import org.koin.androidx.viewmodel.ext.android.viewModel

//组卷（语法+口语）
class AibkGeneratePaperActivity : KBaseActivity(), OnStartDragListener, View.OnClickListener {

    private var layer = 0
    private var stuId = ""
    private var templetType = 6

    companion object {
        fun launch(context: Context, layer: Int, stuId: String) {
            val intent = Intent(context, AibkGeneratePaperActivity::class.java)
            intent.putExtra("Layer", layer)//0语法能力  1语言能力
            intent.putExtra("StuID", stuId)
            context.startActivity(intent)
        }
    }

    private val mViewModel: MarkViewModel by viewModel()
    private var templetPaperInit: TempletPaperInit? = null//试卷名称和ID
    private var templetExceedList: TempletExceedList? = null//试卷难度
    private var templetGrammarSpecificListInfor: TempletGrammarSpecificListInfor? = null//语法专项列表信息
    private var templetPaperListInfor: TempletPaperListInfor? = null//语法组卷模板列表
    private var templetLanguageSpecificListInfor: TempletLanguageSpecificListInfor? = null//语言能力专项列表信息
    private var topicGrammarKlgInfor: TopicGrammarKlgInfor? = null//获取语法专项分析知识点概况
    private var topicLanguageKlgInfor: TopicLanguageKlgInfor? = null//获取语言能力分析知识点概况
    private var mItemTouchHelper: ItemTouchHelper? = null
    private var isCheckGenerateProgress = false;//是否发起获取组卷进度的请求

    private var generateTempletPaperId = ""
    private var generateTempletPaperName = ""
    private var generateTempletLevel = 0
    private var generateTempletId = ""
    private var langId = ""
    private var topicIds = ""

    private var templetIdCheckedStorage: Int = -1//模板列表选中ID暂存

    override fun setLayout(): Int {
        return R.layout.aibk_activity_generate_paper
    }

    override fun initView() {
        if (layer == 0) {
            TvTitle.text = "语法知识专项作业制作"
        } else {
            TvTitle.text = "语言能力专项作业制作"
        }
        BtnBack.setOnClickListener(this)
        BtnCustomTemplet.setOnClickListener(this)
        if (layer == 0) {
            TvMoreSpecific.visibility = View.VISIBLE
            TvMoreSpecific.setOnClickListener(this)
        } else {
            TvMoreSpecific.visibility = View.GONE
        }
        BtnGeneratTestPaperGray.setOnClickListener(this)
        BtnGeneratTestPaperBlue.setOnClickListener(this)
    }

    override fun initData() {
        layer = intent.getIntExtra("Layer", 0)
        stuId = intent.getStringExtra("StuID")

        //获取组卷主页试卷名称及ID
        mViewModel.getPaperInit(stuId, layer)
                .doOnSubscribe {
                    RlGifLoading.visibility = View.VISIBLE
                }.bindLifeCycle(this)
                .subscribe({
                    if (it.code == 1) {
                        RlGifLoading.visibility = View.GONE
                        templetPaperInit = it.data

                        //显示试卷名称及获取PaperID
                        EdtTestName.setText(templetPaperInit!!.PaperName)
                        generateTempletPaperId = templetPaperInit!!.PaperID
                        //显示难度设置信息
                        showDifficultSetting()
                        //显示 0：语法专项 1：语言能力专项
                        showSpecificSetting()
                        //显示 0：语法组卷模板列表 1：语言能力组卷模板列表
                        showPaperTempletSetting(false)//初始化不需要滚动到底部

                    } else {
                        MyToast.showToast(it.message)
                        Handler().postDelayed({
                            finish()
                        }, 500)
                    }
                }, {
                    MyToast.showToast(it.message)
                    Handler().postDelayed({
                        finish()
                    }, 500)
                })
    }

    //获取难度设置信息
    private fun showDifficultSetting() {
        //获取组卷主页试卷名称及ID
        mViewModel.getExceedList()
                .doOnSubscribe {
                }.bindLifeCycle(this)
                .subscribe({
                    if (it.code == 1) {
                        templetExceedList = it.data
                        val adapterDifficultySetting = AibkGenerateDifficultySettingAdapter(this, templetExceedList!!)
                        adapterDifficultySetting.setOnClickListener(object : AibkGenerateDifficultySettingAdapter.OnClickListener {
                            override fun onCheckLevel(level: Int) {
                                generateTempletLevel = level
                            }
                        })
                        RvDifficultySetting.adapter = adapterDifficultySetting
                    } else {
                        MyToast.showToast(it.message)
                    }
                }, {
                    MyToast.showToast(it.message)
                })
    }

    //显示 0：语法专项 1：语言能力专项
    private fun showSpecificSetting() {
        if (layer == 0) {
            //获取语法专项的专题列表
            mViewModel.getTopicList(stuId)
                    .doOnSubscribe {
                    }.bindLifeCycle(this)
                    .subscribe({
                        if (it.code == 1) {
                            templetGrammarSpecificListInfor = it.data

                            var managerGrammarSpecific = LinearLayoutManager(this)
                            managerGrammarSpecific.orientation = LinearLayoutManager.HORIZONTAL
                            RvSpecificChoose.layoutManager = managerGrammarSpecific
                            val mAdapterGrammarSpecific = AibkGenerateSpecificGrammarAdapter(this, templetGrammarSpecificListInfor!!)
                            mAdapterGrammarSpecific.setClickAnalysisListener(object : AibkGenerateSpecificGrammarAdapter.OnClickListener {
                                override fun onClickAnalysis(indexA: Int, indexB: Int, isAllAnalysis: Boolean) {
                                    //先获取专题的知识点信息
                                    getGrammarTopicKlg(indexA, indexB)
                                }

                            })
                            RvSpecificChoose.adapter = mAdapterGrammarSpecific
                            RvSpecificChoose.addItemDecoration(GridSpacingItemDecoration(
                                    3,
                                    ScreenUtils.dip2px(this, 0f), false
                            ))
                        } else {
                            MyToast.showToast(it.message)
                        }
                    }, {
                        MyToast.showToast(it.message)
                    })

        } else {
            //获取语言能力的专题列表
            mViewModel.getAllAbilityBaseInfo(stuId)
                    .doOnSubscribe {
                    }.bindLifeCycle(this)
                    .subscribe({
                        if (it.code == 1) {
                            templetLanguageSpecificListInfor = it.data
                            templetLanguageSpecificListInfor!!.removeAt(1)//暂时屏蔽口语专项选择
                            val languageSpecificListSize = templetLanguageSpecificListInfor!!.size
                            for (i in 0 until languageSpecificListSize) {
                                templetLanguageSpecificListInfor!![i].IsChecked = i == 0
                            }

                            var managerLanguageSpecific = LinearLayoutManager(this)
                            managerLanguageSpecific.orientation = LinearLayoutManager.HORIZONTAL
                            RvSpecificChoose.layoutManager = managerLanguageSpecific
                            val mAdapterLanguageSpecific = AibkGenerateSpecificLanguageAdapter(this, templetLanguageSpecificListInfor!!)
                            mAdapterLanguageSpecific.setClickDialogListener(object : AibkGenerateSpecificLanguageAdapter.OnClickAbilityListener {
                                override fun onClickAbility(pos: Int) {
                                    //重新获取语言能力对应的模板列表数据，并刷新列表
                                    getAbilityTempletInforAndRefresh(pos)
                                }

                                override fun onClickAnalysis(pos: Int) {
//                                    MyToast.showToast("点击了查看语言能力详情")
                                    getLanguageTopicKlg(pos)

                                }
                            })
                            RvSpecificChoose.adapter = mAdapterLanguageSpecific
                            RvSpecificChoose.addItemDecoration(GridSpacingItemDecoration(
                                    templetLanguageSpecificListInfor!!.size,
                                    ScreenUtils.dip2px(this, 0f), false
                            ))

                        } else {
                            MyToast.showToast(it.message)
                        }
                    }, {
                        MyToast.showToast(it.message)
                    })

        }
    }

    private fun getAbilityTempletInforAndRefresh(pos: Int) {
//        templetType = 7 + pos
        if(pos == 0){
            templetType = 7
        }else if(pos == 1){
            templetType = 9
        }else if(pos == 2){
            templetType = 10
        }
        mViewModel.getTempletBriefList(templetType)
                .doOnSubscribe {
                }.bindLifeCycle(this)
                .subscribe({
                    if (it.code == 1) {
                        templetPaperListInfor = it.data
                        mAdapterGrammarTemplet!!.getTempletListInforAndRefresh(templetPaperListInfor!!)
                    } else {
                        MyToast.showToast(it.message)
                    }
                }, {
                    MyToast.showToast(it.message)
                })
    }

    //显示 0：语法组卷模板列表 1：语言能力组卷模板列表
    private var mAdapterGrammarTemplet: AibkGenerateTempletAdapter? = null
    private fun showPaperTempletSetting(scrollToBottom: Boolean) {
        if (layer == 0) {
            templetType = 6//6:语法专项组卷类型
        } else {
            templetType = 7//7-听力能力，8-口语能力，9-阅读能力，10-写作能力
        }
        mViewModel.getTempletBriefList(templetType)
                .doOnSubscribe {
                }.bindLifeCycle(this)
                .subscribe({
                    if (it.code == 1) {
                        templetPaperListInfor = it.data

                        if(templetIdCheckedStorage >=0){
                            val templetPaperListSize = templetPaperListInfor!!.size
                            for(i in 0 until templetPaperListSize){
                                templetPaperListInfor!![i].IsChecked = i == templetIdCheckedStorage
                            }
                        }

                        var managerGrammarTemplet = LinearLayoutManager(this)
                        managerGrammarTemplet.orientation = LinearLayoutManager.VERTICAL
                        RvTempletChoose.layoutManager = managerGrammarTemplet
                        mAdapterGrammarTemplet = AibkGenerateTempletAdapter(this, templetPaperListInfor!!, this)
                        mAdapterGrammarTemplet!!.setOnDelListener(object : AibkGenerateTempletAdapter.OnSwipeListener {
                            override fun onPreview(view: View?, pos: Int) {
                                val templetId = templetPaperListInfor!![pos].TempletID
                                val templetName = templetPaperListInfor!![pos].TempletName
                                val typeCount = templetPaperListInfor!![pos].TypeCount
                                val itemCount = templetPaperListInfor!![pos].ItemCount
                                val fullScore = templetPaperListInfor!![pos].FullScore
                                val answerLength = templetPaperListInfor!![pos].AnswerLength
                                Log.e("mViewModel", "==========传输的templetType========" + templetType)

                                // 0-系统，1-自定义，2-其它用户分享，目前来说没有状态为2的
                                val resource = templetPaperListInfor!![pos].Resource
                                if(resource == 0){
                                    AibkTempletPreviewLookActivity.launch(this@AibkGeneratePaperActivity,
                                            templetId, templetName, typeCount, itemCount, fullScore, answerLength)
                                }else{
                                    templetIdCheckedStorage = -1
                                    if(templetPaperListInfor!=null) {
                                        val templetPaperListSize = templetPaperListInfor!!.size
                                        outLoop@for(i in 0 until templetPaperListSize){
                                            val isChecked = templetPaperListInfor!![i].IsChecked
                                            if(isChecked){
                                                templetIdCheckedStorage = i
                                                break@outLoop
                                            }
                                        }
                                    }

                                    AibkTempletPreviewActivity.launch(this@AibkGeneratePaperActivity,
                                            templetType,templetId, templetName, typeCount, itemCount, fullScore, answerLength)
                                }

                            }

                            override fun onDelete(view: View?, pos: Int, totalCount: Int) {
                                val swipeMenuLayout: SwipeMenuLayout = view as SwipeMenuLayout
                                swipeMenuLayout.quickClose()

                                val templetId = templetPaperListInfor!![pos].TempletID
                                refreshTempletList(templetId, mAdapterGrammarTemplet!!)
                            }

                            override fun onCheck() {
                                var isCheckedTemplet = false
                                val grammarPaperListSize = templetPaperListInfor!!.size
                                outLoop@ for (i in 0 until grammarPaperListSize) {
                                    val isChecked = templetPaperListInfor!![i].IsChecked
                                    if (isChecked) {
                                        isCheckedTemplet = true
                                        generateTempletId = templetPaperListInfor!![i].TempletID
                                        break@outLoop
                                    }
                                }
                                if (isCheckedTemplet) {
                                    ShadowGeneratTestPaperBlue.visibility = View.VISIBLE
                                    ShadowGeneratTestPaperGray.visibility = View.GONE
                                } else {
                                    ShadowGeneratTestPaperBlue.visibility = View.GONE
                                    ShadowGeneratTestPaperGray.visibility = View.VISIBLE
                                }
                            }
                        })
                        RvTempletChoose.adapter = mAdapterGrammarTemplet
                        mItemTouchHelper = ItemTouchHelper(MyItemTouchHelperCallback(mAdapterGrammarTemplet))
                        mItemTouchHelper!!.attachToRecyclerView(RvTempletChoose)

                        if (scrollToBottom) {
                            Sv.post {
                                Sv.fullScroll(View.FOCUS_DOWN);//滚到底部
//                                    Sv.fullScroll(ScrollView.FOCUS_UP);//滚到顶部
                            }
                        }
                    } else {
                        MyToast.showToast(it.message)
                    }
                }, {
                    MyToast.showToast(it.message)
                })
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.BtnBack -> {//返回
                finish()
            }
            R.id.BtnCustomTemplet -> {//自定义模板

                templetIdCheckedStorage = -1
                if(templetPaperListInfor!=null) {
                    val templetPaperListSize = templetPaperListInfor!!.size
                    outLoop@for(i in 0 until templetPaperListSize){
                        val isChecked = templetPaperListInfor!![i].IsChecked
                        if(isChecked){
                            templetIdCheckedStorage = i
                            break@outLoop
                        }
                    }
                }

                AibkCustomTempletActivity.launch(this, templetType)
            }
            R.id.TvMoreSpecific -> {//更多专项
                if (layer == 0) {
                    JumpActivityHelp.jumpToMoreSpecificGrammarActivity(this, templetGrammarSpecificListInfor!!, stuId)
                }
            }
            R.id.BtnGeneratTestPaperGray -> {
                MyToast.showToast("请选择模板后进行组卷")
            }
            R.id.BtnGeneratTestPaperBlue -> {
                generateTempletPaperName = EdtTestName.text.toString()
                if (layer == 0) {
                    val grammarSpecificListSize = templetGrammarSpecificListInfor!!.size
                    for (i in 0 until grammarSpecificListSize) {
                        val subTopicListSize = templetGrammarSpecificListInfor!![i].SubTopicList.size
                        for (j in 0 until subTopicListSize) {
                            val isChecked = templetGrammarSpecificListInfor!![i].SubTopicList[j].IsChecked
                            if (isChecked) {
                                val subTopicId = templetGrammarSpecificListInfor!![i].SubTopicList[j].SubTopicID
                                if (topicIds.isEmpty()) {
                                    topicIds = subTopicId
                                } else {
                                    topicIds = "$topicIds|$subTopicId"
                                }
                            }
                        }
                    }
                } else {
                    val languageSpecificListSize = templetLanguageSpecificListInfor!!.size
                    outLoop@ for (i in 0 until languageSpecificListSize) {
                        val isChecked = templetLanguageSpecificListInfor!![i].IsChecked
                        if (isChecked) {
                            langId = templetLanguageSpecificListInfor!![i].LangID
                            break@outLoop
                        }
                    }
                }

                if (layer == 0) {
                    if (generateTempletPaperName.isEmpty()||topicIds.isEmpty()||generateTempletLevel == 0) {
                        if(generateTempletLevel == 0){
                            MyToast.showToast("请选择难度")
                        }else{
                            if(generateTempletPaperName.isEmpty()){
                                MyToast.showToast("请输入试卷名称")
                            }else{
                                MyToast.showToast("请选择专题")
                            }
                        }
                    } else {
                        //开始组卷
                        mViewModel.startSelected(generateTempletPaperId,
                                stuId, generateTempletLevel, layer, generateTempletId,
                                langId, topicIds, generateTempletPaperName)
                                .doOnSubscribe {
                                    startLoad()
                                }.bindLifeCycle(this)
                                .subscribe({
                                    if (it.code == 1) {
                                        stopLoad()
                                        //发起组卷后，每隔0.5秒获取一次组卷进度进行显示
                                        val dialogGenerateTestpaperProgress = DialogGenerateTestpaperProgress(this)
                                        dialogGenerateTestpaperProgress.show()
                                        dialogGenerateTestpaperProgress.setClickDialogListener(object : DialogGenerateTestpaperProgress.OnClickDialogListener {
                                            override fun onClickGiveUp() {
                                                //放弃组卷
                                                isCheckGenerateProgress = false
                                                deletePaper()
                                            }
                                        })

                                        isCheckGenerateProgress = true
                                        getGenerateProgress(dialogGenerateTestpaperProgress)

                                    } else {
                                        stopLoad()
                                        MyToast.showToast(it.message)
                                    }
                                }, {
                                    stopLoad()
                                    MyToast.showToast(it.message)
                                })
                    }
                }else{
                    if (generateTempletPaperName.isEmpty()||langId.isEmpty()||generateTempletLevel == 0) {
                        if(generateTempletLevel == 0){
                            MyToast.showToast("请选择难度")
                        }else{
                            if(generateTempletPaperName.isEmpty()){
                                MyToast.showToast("请输入试卷名称")
                            }else{
                                MyToast.showToast("请选择专题")
                            }
                        }
                    } else {
                        //开始组卷
                        mViewModel.startSelected(generateTempletPaperId,
                                stuId, generateTempletLevel, layer, generateTempletId,
                                langId, topicIds, generateTempletPaperName)
                                .doOnSubscribe {
                                    startLoad()
                                }.bindLifeCycle(this)
                                .subscribe({
                                    if (it.code == 1) {
                                        stopLoad()
                                        //发起组卷后，每隔0.5秒获取一次组卷进度进行显示
                                        val dialogGenerateTestpaperProgress = DialogGenerateTestpaperProgress(this)
                                        dialogGenerateTestpaperProgress.show()
                                        dialogGenerateTestpaperProgress.setClickDialogListener(object : DialogGenerateTestpaperProgress.OnClickDialogListener {
                                            override fun onClickGiveUp() {
                                                //放弃组卷
                                                isCheckGenerateProgress = false
                                                deletePaper()
                                            }
                                        })

                                        isCheckGenerateProgress = true
                                        getGenerateProgress(dialogGenerateTestpaperProgress)

                                    } else {
                                        stopLoad()
                                        MyToast.showToast(it.message)
                                    }
                                }, {
                                    stopLoad()
                                    MyToast.showToast(it.message)
                                })
                    }
                }
            }
        }
    }

    private fun getGenerateProgress(dialogGenerateTestpaperProgress: DialogGenerateTestpaperProgress) {
        if (isCheckGenerateProgress) {
            mViewModel.getSelectedProgress(generateTempletPaperId, stuId, layer, generateTempletId)
                    .doOnSubscribe {
                    }.bindLifeCycle(this)
                    .subscribe({
                        if (it.code == 1) {
                            val progress = it.data

//                            dialogGenerateTestpaperProgress.showGenerateProgressMessage(progress)
//                            if (progress == "100") {
//                                Handler().postDelayed({//500毫秒后,生成试卷（抽题结束后，调用接口，生成试卷结构信息）
//                                    mViewModel.generatePaper(generateTempletPaperId, generateTempletPaperName,
//                                            stuId, layer, generateTempletId)
//                                            .doOnSubscribe {
//                                            }.bindLifeCycle(this)
//                                            .subscribe({
//                                                if (it.code == 1) {
//                                                    dialogGenerateTestpaperProgress.dismiss()
////                                                    MyToast.showToast("接下来该跳转到试卷审核界面")
//                                                    AibkTestPaperReviewActvity.launch(this, generateTempletPaperId, stuId)
//                                                } else {
//                                                    MyToast.showToast(it.message)
//                                                }
//                                            }, {
//                                                MyToast.showToast(it.message)
//                                            })
//                                }, 500)
//                            } else {
//                                //抽题进度未达100，就重新获取抽题进度进行显示
//                                getGenerateProgress(dialogGenerateTestpaperProgress)
//                            }
//                        } else {
//                            MyToast.showToast(it.message)
//                        }

                            if (progress == "1000") {
                                Handler().postDelayed({//500毫秒后,生成试卷（抽题结束后，调用接口，生成试卷结构信息）
                                    dialogGenerateTestpaperProgress.dismiss()
                                    //MyToast.showToast("接下来该跳转到试卷审核界面")
                                    AibkTestPaperReviewActvity.launch(this,layer,generateTempletPaperId, stuId)
                                }, 100)
                            } else {
                                //抽题进度未达100，就重新获取抽题进度进行显示
                                dialogGenerateTestpaperProgress.showGenerateProgressMessage(progress)
                                getGenerateProgress(dialogGenerateTestpaperProgress)
                            }
                        } else {
                            MyToast.showToast(it.message)
                        }

                    }, {
                        MyToast.showToast(it.message)
                    })
        }
    }


    /*
  * 删除未发布的试卷
  * */
    private fun deletePaper() {
        mViewModel.delPaper(generateTempletPaperId,
                stuId, layer)
                .doOnSubscribe {
                }.bindLifeCycle(this)
                .subscribe({
                    if (it.code == 1) {
                        //放弃组卷成功
                        //MyToast.showToast(it.message)
                    } else {
                        MyToast.showToast(it.message)
                    }
                }, {
                    MyToast.showToast(it.message)
                })
    }

    override fun onStartDrag(viewHolder: RecyclerView.ViewHolder?) {
        //通知ItemTouchHelper开始拖拽
        mItemTouchHelper!!.startDrag(viewHolder!!)
    }

    private fun refreshTempletList(templetId: String, mAdapterGrammarTemplet: AibkGenerateTempletAdapter) {
        mViewModel.delCustomizeTemplet(templetId)
                .doOnSubscribe {
                    startLoad()
                }.bindLifeCycle(this)
                .subscribe({
                    if (it.code == 1) {
                        stopLoad()
                        Handler().postDelayed({
                            mAdapterGrammarTemplet.refreshView()
                        }, 500)
                    } else {
                        MyToast.showToast(it.message)
                    }
                }, {
                    MyToast.showToast(it.message)
                })
    }

    private fun getGrammarTopicKlg(indexA: Int, indexB: Int) {

        val topicIdsX = templetGrammarSpecificListInfor!![indexA].SubTopicList[indexB].SubTopicID
        mViewModel.getTopicKlg(stuId, topicIdsX)
                .doOnSubscribe {
                    startLoad()
                }.bindLifeCycle(this)
                .subscribe({
                    if (it.code == 1) {
                        stopLoad()
                        topicGrammarKlgInfor = it.data
                        val dialogSpecificGrammarAnalysis = DialogSpecificGrammarAnalysis(
                                this@AibkGeneratePaperActivity,
                                this@AibkGeneratePaperActivity,
                                templetGrammarSpecificListInfor!!,
                                indexA,
                                indexB,
                                false,
                                topicGrammarKlgInfor!!)
                        dialogSpecificGrammarAnalysis.show()
                    } else {
                        MyToast.showToast(it.message)
                    }
                }, {
                    MyToast.showToast(it.message)
                })
    }

    private fun getLanguageTopicKlg(pos: Int) {

        val langId = templetLanguageSpecificListInfor!![pos].LangID
        mViewModel.getLangKlgList(stuId, langId)
                .doOnSubscribe {
                    startLoad()
                }.bindLifeCycle(this)
                .subscribe({
                    if (it.code == 1) {
                        stopLoad()
                        topicLanguageKlgInfor = it.data
                        val dialogSpecificLanguageAnalysis = DialogSpecificLanguageAnalysis(
                                this@AibkGeneratePaperActivity,
                                this@AibkGeneratePaperActivity,
                                pos,
                                topicLanguageKlgInfor!!)
                        dialogSpecificLanguageAnalysis.show()
                    } else {
                        MyToast.showToast(it.message)
                    }
                }, {
                    MyToast.showToast(it.message)
                })
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 101) {//从语法专项选择页面返回到组卷主页面时，刷新界面中语法专项选中状态
            RvSpecificChoose.adapter!!.notifyDataSetChanged()
        } else if (requestCode == 202) {
            showPaperTempletSetting(true)//重现请求模板数据，自动滚动到底部
        } else if (requestCode == 303) {
            showPaperTempletSetting(true)//重现请求模板数据，自动滚动到底部
        }
    }

}